package com.walker.springboot.rabbitmq.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.rabbitmq.tools.json.JSONUtil;
import com.walker.springboot.rabbitmq.dao.UserDao;
import com.walker.springboot.rabbitmq.entity.User;
import com.walker.springboot.rabbitmq.listener.MyEvent;
import com.walker.springboot.rabbitmq.response.BaseResponse;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @ProjectName: walker-step
 * @Package: com.walker.springboot.rabbitmq.controller
 * @ClassName: HelloController
 * @Author: yangyl
 * @CreateDate: 2020/6/21 18:46
 */
@RestController
public class HelloController {

    @Autowired
    protected UserDao userDao;
    @Autowired
    protected ApplicationEventPublisher applicationEventPublisher;

    @Autowired
    protected RabbitTemplate rabbitTemplate;

    @Autowired
    protected Environment environment;

    private static ObjectMapper mapper = new ObjectMapper();

    @GetMapping("/hello")
    public BaseResponse hello() {
        BaseResponse baseResponse = new BaseResponse();
        Map<String, Object> data = userDao.select("1");
        baseResponse.setData(data);
        baseResponse.setCode("100");
        baseResponse.setMessage("success");
        return baseResponse;
    }

    @GetMapping("order")
    public BaseResponse order() {
        BaseResponse baseResponse = new BaseResponse();
        User user=new User();
        user.setId("4");
        user.setName("赵刘来了。。");
        MyEvent my=new MyEvent(this,user);
        applicationEventPublisher.publishEvent(my);
        baseResponse.setCode("100");
        baseResponse.setMessage("success");
        return baseResponse;
    }


    @GetMapping("sendSimple")
    public BaseResponse sendSimple() throws Exception {
        BaseResponse baseResponse = new BaseResponse();
        String exchange = environment.getProperty("rabbitmq.config.basic.exchange-key");
        String rougint = environment.getProperty("rabbitmq.config.basic.routing-key");
        Map m=new HashMap();
        m.put("id",1);
        m.put("name","zjamgsam");
        String s = mapper.writeValueAsString(m);
        Message mes = MessageBuilder.withBody(s.getBytes()).build();
        rabbitTemplate.send(exchange,rougint,mes);
        baseResponse.setCode("100");
        baseResponse.setMessage("success");
        return baseResponse;
    }

    @GetMapping("sendTtlMsg")
    public BaseResponse sendTtlMsg() throws Exception {
        BaseResponse baseResponse = new BaseResponse();
        String exchange ="ttl.exchange";
        Map m=new HashMap();
        m.put("id",1);
        m.put("name","zjamgsam");
        String s = mapper.writeValueAsString(m);
        Message mes = MessageBuilder.withBody(s.getBytes()).setDeliveryMode(MessageDeliveryMode.PERSISTENT).build();
        rabbitTemplate.send(exchange,"xx",mes);
        baseResponse.setCode("100");
        baseResponse.setMessage("success");
        return baseResponse;
    }
}
